package city.spring.modules.cardinfo.service.impl;

import city.spring.modules.cardinfo.entity.CardEmployeeEntity;
import city.spring.modules.cardinfo.entity.PhysicalCardChangeLogEmEntity;
import city.spring.modules.cardinfo.repository.CardEmployeeRepository;
import city.spring.modules.cardinfo.repository.PhysicalCardChangeLogEmRepository;
import city.spring.modules.cardinfo.service.CardEmployeeService;
import city.spring.modules.cardinfo.service.PhysicalCardChangeLogEmService;
import city.spring.modules.cardinfo.vo.CardEmployeeEntityVO;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * Service：卡号信息
 *
 * @author pxm
 * @date 2020-12-25 09:18:46
 */
@CacheConfig(cacheNames = {CardEmployeeServiceImpl.CACHE_NAME})
@Transactional(rollbackFor = Exception.class)
@Service
public class CardEmployeeServiceImpl extends ServiceImpl<CardEmployeeRepository, CardEmployeeEntity> implements CardEmployeeService {
    private final PhysicalCardChangeLogEmService physicalCardChangeLogEmService;
    private final PhysicalCardChangeLogEmRepository physicalCardChangeLogEmRepository;

    public CardEmployeeServiceImpl(PhysicalCardChangeLogEmService physicalCardChangeLogEmService, PhysicalCardChangeLogEmRepository physicalCardChangeLogEmRepository) {
        this.physicalCardChangeLogEmService = physicalCardChangeLogEmService;
        this.physicalCardChangeLogEmRepository = physicalCardChangeLogEmRepository;
    }

    @Override
    public void saveCardEmployee(CardEmployeeEntity entity) {
        if (!save(entity)) {
            throw new RuntimeException("保存信息失败");
        }
    }

    @Override
    public void updateCardEmployee(CardEmployeeEntity entity) {
        // 使用 updateById(entity); 修改数据时，将会修改 entity 对象中所有非null数据，如果某个字段为null，将会忽略该字段的修改
        if (!updateById(entity)) {
            throw new RuntimeException("修改信息失败");
        }
    }

    @Override
    public void deleteCardEmployee(String primaryKey) {
        removeById(primaryKey);
    }

    @Override
    public void deleteCardEmployee(List<String> primaryKeys) {
        removeByIds(primaryKeys);
    }

    @Override
    public IPage<CardEmployeeEntityVO> staffListPage(Page<? extends CardEmployeeEntity> page, Wrapper<? extends CardEmployeeEntity> wrapper) {
        return baseMapper.staffListPage(page, wrapper);
    }

    @Override
    public void updatePhysics(CardEmployeeEntity entity) {
        // 更改卡片物理卡号的同时，保存更换记录   涉及两个表：卡片信息表、更换记录表
        // 方式一：第一次绑定物理卡号，不计入更换记录
        PhysicalCardChangeLogEmEntity physicalCardChangeLogEmEntity = new PhysicalCardChangeLogEmEntity(); // 物理卡更换记录  设置物理卡更换记录的内容（物理卡更换记录表）
        physicalCardChangeLogEmEntity.setOldCard(entity.getLastPhysicsCard()); // 最后更新的物理卡号赋给旧卡号
        entity.setLastPhysicsCard(entity.getPhysicsCard()); // 设置 现物理卡号 与 最后更新的物理卡号  一致
        physicalCardChangeLogEmEntity.setPersonId(entity.getPerson());
        physicalCardChangeLogEmEntity.setPersonType(entity.getPersonType());
        physicalCardChangeLogEmEntity.setNewCard(entity.getPhysicsCard());
        physicalCardChangeLogEmEntity.setReplaceTime(LocalDateTime.now());
        int countChange = physicalCardChangeLogEmRepository.changeCount(physicalCardChangeLogEmEntity.getPersonId()); // 计算累积更换次数
        physicalCardChangeLogEmEntity.setCounts(countChange + 1); // countChange：这次之前的更换次数
        if (!(physicalCardChangeLogEmEntity.getOldCard() == null) && !(physicalCardChangeLogEmEntity.getNewCard() == null)) {
            physicalCardChangeLogEmService.savePhysicalCardChangeLogEm(physicalCardChangeLogEmEntity);
        }
        this.updateById(entity);
        if (!entity.isCompletion()) {
            throw new RuntimeException("一卡通信息更新失败");
        }
    }
}